// pages/mine/classList/addClass/addClass.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    isStu: false,
    isTea: false,
    lesson: '',
    class: '',
    Students: []
  },

  getlesson(e){
    this.setData({
      lesson: e.detail.value,
    })
  },

  getclass(e){
    this.setData({
      class: e.detail.value,
    })
  },

  

  joinlesson(){
    const db = wx.cloud.database()
    let that = this
    const _=db.command
    let userInfo = wx.getStorageSync('userInfo')
    let _cid = ''
    let isjoin =false
    db.collection('classInfo').where({
      lessonName: that.data.lesson,
      className: that.data.class,
    })
    .get().then(res =>{
      _cid = res.data[0]._id
      userInfo.classinfo.forEach((i) =>{
        if(i.cid == _cid){
          wx.showToast({
            title: '已经在了喔',
            duration: 1000
          })
          isjoin = true
        }
      })
      if(isjoin) return
      db.collection('classInfo').doc(res.data[0]._id)
      .update({
        data:{
          students: _.push({
            sId: userInfo._id,
            sName: userInfo.nickName,
            avatarUrl: userInfo.avatarUrl,
          })
        }
      })
      .then(res =>{
        console.log(res)
        db.collection('userInfo').doc(userInfo._id).update({
          data:{
            classinfo: _.push({
              cid: _cid,
              className: that.data.class,
              lessonName: that.data.lesson,
            })
          }
        })
        .then(res =>{
          wx.showToast({
            title: '加入成功',
          })
          wx.navigateTo({
            url: '../classList',
          })
        })
      })
  })
},



  addlesson(){
    const db = wx.cloud.database()
    let userInfo = wx.getStorageSync('userInfo')
    const _=db.command
    let that = this
    db.collection('classInfo').add({
      data:{
        TId : userInfo._id,
        TName : userInfo.nickName,
        className: that.data.class,
        lessonName: that.data.lesson,
        students: that.data.Students,
        avatarUrl: userInfo.avatarUrl,
      }
    })
    .then(res =>{
      console.log(res)
      db.collection('userInfo').doc(userInfo._id).update({
        data:{
          classinfo: _.push({
            cid: res._id,
            className: that.data.class,
            lessonName: that.data.lesson,
          })
        }
      })
      .then(res =>{
        wx.showToast({
          title: '创建成功',
        })
        wx.navigateTo({
          url: '../classList',
        })
      })
    })
    

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let userInfo = wx.getStorageSync('userInfo');
    if(userInfo.status == 'teacher'){
      this.setData({
        isTea: true,
      })
    }
    else{
      this.setData({
        isStu: true,
      })
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})